package drawing.demo;
import android.content.Context;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.Paint.FontMetrics;  
import android.util.AttributeSet;
import android.util.Log;  
import android.view.View;  
  
public class TextDraw extends View {  
  
	public TextDraw(Context context) {  
        super(context);  
    }  
  
    public TextDraw(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
    
    public TextDraw(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);  
        textPaint.setTextSize( 35);  
        textPaint.setColor( Color.WHITE);  
  
        // FontMetrics  
        FontMetrics fontMetrics = textPaint.getFontMetrics();  
  
        String text = "ABCDEFGHIJKLMNOPQRST";  
  
        //   
        float baseX = 0;  
        float baseY = 100;  
        Log.d("textDraw", "top = "+fontMetrics.top+  
                                        "ascent = "+fontMetrics.ascent+  
                                        "descent = "+fontMetrics.descent+  
                                        "bottom = "+fontMetrics.bottom+"\n");  
        float topY = baseY + fontMetrics.top;  
        float ascentY = baseY + fontMetrics.ascent;  
        float descentY = baseY + fontMetrics.descent;  
        float bottomY = baseY + fontMetrics.bottom;  
        Log.d("textDraw", "topY = "+topY+  
                                        "ascentY = "+ascentY+  
                                        "descentY = "+descentY+  
                                        "bottomY = "+bottomY);  
        //   
        canvas.drawText( text, baseX, baseY, textPaint);  
  
        // BaseLine  
        Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);  
        baseLinePaint.setColor( Color.RED);  
        canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);  
  
        // Base  
        canvas.drawCircle( baseX, baseY, 5, baseLinePaint);  
  
        // TopLine  
        Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);  
        topLinePaint.setColor( Color.LTGRAY);  
        canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);  
  
        // AscentLine  
        Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);  
        ascentLinePaint.setColor( Color.GREEN);  
        canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);  
  
        // DescentLine  
        Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);  
        descentLinePaint.setColor( Color.YELLOW);  
        canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);  
  
        // ButtomLine  
        Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);  
        bottomLinePaint.setColor( Color.MAGENTA);  
        canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);  
    }  
}  